package com.fare.taluo.core.domain.freightprice;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.hadoop.hbase.KeyValue;

import com.fare.taluo.util.DateUtils;
/**
 * 运价航班实体类
 * @author creator: xiangsf
 * @author modifior: xiangsf
 * @since 2012-12-25-下午4:23:34
 * @version v1.0
 */
public class FreightFlightNoEntity extends FreightFlightDayEntity {
	/**
	 * 起飞时间
	 */
	private Date fromDate;
	/**
	 * 到达时间
	 */
	private Date toDate;
	/**
	 * 航班号
	 */
	private String flightNumber;
	/**
	 * 机型
	 */
	private String planeType;

	public String getFlightNumber() {
		return flightNumber;
	}
	public void setFlightNumber(String flightNumber) {
		this.flightNumber = flightNumber;
	}
	public String getPlaneType() {
		return planeType;
	}
	public void setPlaneType(String planeType) {
		this.planeType = planeType;
	}
	public Date getFromDate() {
		return fromDate;
	}
	public void setFromDate(Date fromDate) {
		this.fromDate = fromDate;
	}
	public Date getToDate() {
		return toDate;
	}
	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}
	@Override
	public FreightFlightEntity createEntity(KeyValue kv) throws Exception {
		String family= null,qualifier = null,value = null;
		try{
				super.createEntity(kv);
				 family = new String(kv.getFamily(), "utf-8");
				 qualifier = new String(kv.getQualifier(), "utf-8");
				 value = new String(kv.getValue(), "utf-8");
				if("planeinfo".equalsIgnoreCase(family)){
					if("flightBatchStartTime".equalsIgnoreCase(qualifier)){
						this.fromDate = this.getFlightStartTime(DateFormatUtils.format(this.getFlightDate(), "yyyy-MM-dd"), value);
					}else if("flightBatchEndTime".equalsIgnoreCase(qualifier)){
						this.toDate = this.getFlightStartTime(DateFormatUtils.format(this.getFlightDate(), "yyyy-MM-dd"), value);
					}else if("flightId".equalsIgnoreCase(qualifier)){
						this.flightNumber = value;
					}else if("type".equalsIgnoreCase(qualifier)){
						this.planeType = value;
					} 
				}
		}finally{
			 qualifier = null;
			 family = null;
			 value = null;
		}
		return this;
	}
	/**
	 * 得到起飞时间
	 * <p>由flightBatchDate+flightBatchStartTime组合而成
	 * @return
	 * @throws ParseException 
	 */
	public Date getFlightStartTime(String flightBatchDate,String flightBatchStartTime) throws ParseException{
		if(StringUtils.isNotEmpty(flightBatchDate) && StringUtils.isNotEmpty(flightBatchStartTime)){
			String time = flightBatchStartTime.indexOf(":") > -1 ? flightBatchStartTime.replace(":", "") : flightBatchStartTime;
			if(time.length() == 3)  time = "0"+time;
			else if(time.length() == 2) time = "00"+time;
			else if(time.length() == 1) time = "000"+time;
			return DateUtils.parseDate(flightBatchDate+" "+time, "yyyy-MM-dd HHmm");
		}
		return null;
	}
	/**
	 * 得到起飞时间
	 * <p>由flightBatchDate+flightBatchEndTime组合而成,暂不考虑跨天了
	 * @return
	 * @throws ParseException 
	 */
	public Date getFlightEndTime(String flightBatchDate, String flightBatchEndTime) throws ParseException{
		if(StringUtils.isNotEmpty(flightBatchDate) && StringUtils.isNotEmpty(flightBatchEndTime)){
			String time = flightBatchEndTime.indexOf(":") > -1 ? flightBatchEndTime.replace(":", "") : flightBatchEndTime;
			if(time.length() == 3)  time = "0"+time;
			else if(time.length() == 2) time = "00"+time;
			else if(time.length() == 1) time = "000"+time;
			return DateUtils.parseDate(flightBatchDate+" "+time, "yyyy-MM-dd HHmm");
		}
		return null;
	}
	
}
